GBML manual / Events

collision

Runs once for each instance of the target object this instance overlaps, after every collision in the frame has been found first. Inside it, `other` is the instance that was hit.

Syntax

collision: Collision with <object> - fires once per overlapping instance, after every collision is found

Description

Runs when this instance touches an instance of another object. You pick the other object when you add the event: "Collision with obj_gem" runs for an instance that touches a gem.

It runs once for each instance it touches. If a hero touches three gems at the same time, the event runs three times, one for each gem. If it touches none, it does not run.

Inside the event, self is this instance and other is the instance it touched. Use other to read or change the instance that was hit. To run several lines on it, or on this instance, write with (other) { ... } (or with (self) { ... }): inside the block self is the instance that was hit and other is the one that was running. The same works in an Attack hits event.

An object can have a separate Collision event for each object it cares about. Each one is its own file and its own piece of code.

What "touching" means

Every instance has a collision box, a rectangle. Two instances touch when their boxes overlap. Boxes that only share an edge do not touch.

An instance with no collision box (no sprite and no mask) has nothing to touch with, so it never gets a Collision event and it is never the other in one; the same goes for a box that has a width but no height, or a height but no width. The pos_meeting and collides functions use the same boxes but are not that strict: they do meet a point that is strictly inside a box (see Overlap and collision).

An event matches the object you named and its children. If you write "Collision with obj_enemy" and obj_slime is a child of obj_enemy, the event runs when you touch an obj_enemy and when you touch an obj_slime. other is the one you touched, and you can use everything obj_enemy has through it. It only goes down: "Collision with obj_slime" does not run for a plain obj_enemy. If an object has an event for a parent and another for one of its children, touching that child runs both. See Objects and parents.

Found first, then run

Every frame does two things, in this order:

  1. The engine finds every touching pair.
  2. Then it runs the events, one after another.

Because the list is made first, nothing your events do can change it. If one event moves an instance away, the other events for this frame still run. They were already decided.

Collision events run at step 7 of the order of a frame: after movement, before Step End.

Who runs first

The order is always the same, so you can count on it:

  1. Objects go in the order they are listed in your project.
  2. One object's Collision events go in alphabetical order of the object they name.
  3. For one event, the instances of this object go in the order Step runs them. For each of those, the instances it touches go in the same order.
  4. If the event names a parent, the touched instances go one kind at a time: the named object's own first, then each child kind in the order your project lists them.

If obj_a has a Collision event for obj_b and obj_b has one for obj_a, both run for the same touch. All of obj_a's events run first if obj_a comes first in your project, and all of obj_b's after them.

An object can also have a Collision event for itself. Then each instance runs the event for every other instance it touches. An instance never touches itself.

When an instance is destroyed

instance_destroy takes effect for collisions at once, even though the instance finishes the rest of its frame. If an instance is destroyed earlier in the frame, its Collision events do not run, and it is not the other of anyone else's. Events that were waiting for it are skipped. Everything else runs as normal.

That matters when both objects have an event for each other. Say a bullet destroys itself in its own Collision event, and the enemy also has a Collision event for bullets. If the bullet's event runs first, the bullet is gone, so the enemy's event is skipped. Put the code you want to always run in the event that runs first, or do not destroy things until the last event.

An instance you create inside a Collision event is not tested until the next frame.

Limits

Example

instance_destroy(other);

In a hero's "Collision with obj_gem". Each gem the hero touches is destroyed, once each. Touch three at once and the event runs three times.

instance_destroy(other);
instance_destroy(self);

In a bullet's "Collision with obj_enemy". The bullet and the enemy it hit are both destroyed. A bullet that touches two enemies in one frame destroys the first, then is itself gone, so the second enemy is skipped for it.

if (other.y > y) {
    vsp = -4;
}

In a hero's "Collision with obj_spikes". other.y reads the spikes' position. If the spikes are below the hero, the hero is pushed up.

On the GBA

Costs only for pairs you write an event for: live instances of this object times live instances of the target, one rectangle test each. An object with no Collision event costs nothing, however many instances it has. If the target has children, each child kind is a pair of its own (each has its own pool), and `other` is typed as the named target.

See also: EventsThe order of a framecollidespos_meetinginstance_destroyObjects and parents